<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mime.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_i18n_mime.js.html">mime.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Functions for encoding strings according to MIME
<a name="line17"></a> * standards, especially RFC 1522.
<a name="line18"></a> */
<a name="line19"></a>goog.provide(&#39;goog.i18n.mime&#39;);
<a name="line20"></a>goog.provide(&#39;goog.i18n.mime.encode&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.array&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>/**
<a name="line26"></a> * Regular expression for matching those characters that are outside the
<a name="line27"></a> * range that can be used in the quoted-printable encoding of RFC 1522:
<a name="line28"></a> * anything outside the 7-bit ASCII encoding, plus ?, =, _ or space.
<a name="line29"></a> * @type {RegExp}
<a name="line30"></a> * @private
<a name="line31"></a> */
<a name="line32"></a>goog.i18n.mime.NONASCII_ = /[^!-&lt;&gt;@-^`-~]/g;
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>/**
<a name="line36"></a> * Like goog.i18n.NONASCII_ but also omits double-quotes.
<a name="line37"></a> * @type {RegExp}
<a name="line38"></a> * @private
<a name="line39"></a> */
<a name="line40"></a>goog.i18n.mime.NONASCII_NOQUOTE_ = /[^!#-&lt;&gt;@-^`-~]/g;
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Encodes a string for inclusion in a MIME header. The string is encoded
<a name="line45"></a> * in UTF-8 according to RFC 1522, using quoted-printable form.
<a name="line46"></a> * @param {string} str The string to encode.
<a name="line47"></a> * @param {boolean=} opt_noquote Whether double-quote characters should also
<a name="line48"></a> *     be escaped (should be true if the result will be placed inside a
<a name="line49"></a> *     quoted string for a parameter value in a MIME header).
<a name="line50"></a> * @return {string} The encoded string.
<a name="line51"></a> */
<a name="line52"></a>goog.i18n.mime.encode = function(str, opt_noquote) {
<a name="line53"></a>  var nonascii = opt_noquote ?
<a name="line54"></a>      goog.i18n.mime.NONASCII_NOQUOTE_ : goog.i18n.mime.NONASCII_;
<a name="line55"></a>
<a name="line56"></a>  if (str.search(nonascii) &gt;= 0) {
<a name="line57"></a>    str = &#39;=?UTF-8?Q?&#39; + str.replace(nonascii,
<a name="line58"></a>        /**
<a name="line59"></a>         * @param {string} c The matched char.
<a name="line60"></a>         * @return {string} The quoted-printable form of utf-8 encoding.
<a name="line61"></a>         */
<a name="line62"></a>        function(c) {
<a name="line63"></a>          var i = c.charCodeAt(0);
<a name="line64"></a>          if (i == 32) {
<a name="line65"></a>            // Special case for space, which can be encoded as _ not =20
<a name="line66"></a>            return &#39;_&#39;;
<a name="line67"></a>          }
<a name="line68"></a>          var a = goog.array.concat(&#39;&#39;, goog.i18n.mime.getHexCharArray(c));
<a name="line69"></a>          return a.join(&#39;=&#39;);
<a name="line70"></a>        }) + &#39;?=&#39;;
<a name="line71"></a>  }
<a name="line72"></a>  return str;
<a name="line73"></a>};
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Get an array of UTF-8 hex codes for a given character.
<a name="line78"></a> * @param {string} c The matched character.
<a name="line79"></a> * @return {!Array.&lt;string&gt;} A hex array representing the character.
<a name="line80"></a> */
<a name="line81"></a>goog.i18n.mime.getHexCharArray = function(c) {
<a name="line82"></a>  var i = c.charCodeAt(0);
<a name="line83"></a>  var a = [];
<a name="line84"></a>  // First convert the UCS-2 character into its UTF-8 bytes
<a name="line85"></a>  if (i &lt; 128) {
<a name="line86"></a>    a.push(i);
<a name="line87"></a>  } else if (i &lt;= 0x7ff) {
<a name="line88"></a>    a.push(
<a name="line89"></a>        0xc0 + ((i &gt;&gt; 6) &amp; 0x3f),
<a name="line90"></a>        0x80 + (i &amp; 0x3f));
<a name="line91"></a>  } else if (i &lt;= 0xffff) {
<a name="line92"></a>    a.push(
<a name="line93"></a>        0xe0 + ((i &gt;&gt; 12) &amp; 0x3f),
<a name="line94"></a>        0x80 + ((i &gt;&gt; 6) &amp; 0x3f),
<a name="line95"></a>        0x80 + (i &amp; 0x3f));
<a name="line96"></a>  } else {
<a name="line97"></a>    // (This is defensive programming, since ecmascript isn&#39;t supposed
<a name="line98"></a>    // to handle code points that take more than 16 bits.)
<a name="line99"></a>    a.push(
<a name="line100"></a>        0xf0 + ((i &gt;&gt; 18) &amp; 0x3f),
<a name="line101"></a>        0x80 + ((i &gt;&gt; 12) &amp; 0x3f),
<a name="line102"></a>        0x80 + ((i &gt;&gt; 6) &amp; 0x3f),
<a name="line103"></a>        0x80 + (i &amp; 0x3f));
<a name="line104"></a>  }
<a name="line105"></a>  // Now convert those bytes into hex strings (don&#39;t do anything with
<a name="line106"></a>  // a[0] as that&#39;s got the empty string that lets us use join())
<a name="line107"></a>  for (i = a.length - 1; i &gt;= 0; --i) {
<a name="line108"></a>    a[i] = a[i].toString(16);
<a name="line109"></a>  }
<a name="line110"></a>  return a;
<a name="line111"></a>};
</pre>


</body>
</html>
